﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.BusinessLogical.Model;
using DispatchAssist.BusinessLogical.DAL;
using DispatchAssist.Model;

namespace DispatchAssist.BusinessLogical.BLL
{
    public class CreateAlertMsgManager
    {

        private IDataProvider _provider;

        public CreateAlertMsgManager()
        {
            _provider = new SqlDataProvider();
        }


        #region Common Alert Info
        /// <summary>
        /// 获取未生成预警的最新气象报文
        /// </summary>
        /// <returns></returns>
        public List<MeteoMsg> GetUnCreateMeteoMsgList(Enums.MeteoMsgStatus status, Enums.MeteoMsgStatus exceptStatus)
        {
            return _provider.GetUnCreateMeteoMsgList((int)status, (int)exceptStatus);
        }

        /// <summary>
        /// 根据机场,时间段条件筛选符合条件的航班信息
        /// </summary>
        /// <param name="airPort"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<FlightInfo> GetFligetInfoByMtMsg(string airPort, DateTime effectTime)
        {
            return _provider.GetFligetInfoByMtMsg(airPort, effectTime);
        }

        /// <summary>
        /// 获取机场的报警规则
        /// </summary>
        /// <param name="airPort"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public List<AlertRule> GetAlertRuleByPara(string airPort, int level)
        {
            return _provider.GetAlertRuleByPara(airPort, level);
        }

        /// <summary>
        /// 缓存所有报警规则参数
        /// </summary>
        /// <returns></returns>
        public CacheAlertRule GetAllAlertRule()
        {
            List<AlertRule> list = _provider.GetAlertRuleByPara(null, -1);

            return new CacheAlertRule(list);
        }

        /// <summary>
        /// 缓存所有机场报警提前时间参数
        /// </summary> 
        /// <returns></returns>
        public CacheEarlyTime GetAllEarlyWarn()
        {
            List<EarlyWarning> list = _provider.GetEarlyTimeByPara(null);

            return new CacheEarlyTime(list);
        }

        /// <summary>
        /// 缓存所有席位机号对应关系参数
        /// </summary>
        /// <returns></returns>
        public CacheSeatTaiNo GetAllSeatTailNo()
        {
            List<SeatMonitor> list = _provider.GetSeatMonitorByPara(null, null);

            return new CacheSeatTaiNo(list);

        }

        /// <summary>
        /// 插入预警信息列表
        /// </summary>
        /// <param name="modelList"></param>
        public void InsertAlertModelList(List<Alert> modelList)
        {
            foreach (Alert model in modelList)
            {
                InsertAlertModel(model);
            }
        }

        /// <summary>
        /// 插入一条预警信息
        /// </summary>
        /// <param name="model"></param>
        public Alert InsertAlertModel(Alert model)
        {
            return _provider.CreateAlert(model);
        }

        /// <summary>
        /// 更新气象报文状态
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="status"></param>
        public void UpdateMeteoStatus(List<string> Ids, Enums.MeteoMsgStatus status)
        {
            if (Ids.Count > 0)
            {
                _provider.UpdateMeteoStatus(Ids, status);
            }
        }

        #endregion


        #region SMS Alert Info

        public List<FlightInfo> GetSMSFlightInfoByMtMsg(string airport, DateTime startTime, DateTime endTime)
        {
            return _provider.GetSMSFlightInfoByMtMsg(airport, startTime, endTime);
        }

        public void InsertSMSAlertModel(SMSAlert model)
        {
            _provider.InsertSMSAlertModel(model);
        }

        public void InsertSMSAlertList(List<SMSAlert> list)
        {
            foreach (SMSAlert model in list)
            {
                InsertSMSAlertModel(model);
            }
        }


        public bool IsEffective(string airport, DateTime warntime, int level,Guid msgid)
        {
            bool result = true;
            try
            {
                List<MeteoMsg> list = _provider.GetMeteoListByAirportAndLevel(airport, warntime, level, msgid);
                if (list.Count > 0)
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                CarolLib.Log.Instance.Warn("Error during IsEffective ", e);
                result = true;
            }

            return result;
        }

        #endregion

        #region common

        public static Enums.AlertType GetAlertType(Enums.AlertActionFlag flags)
        {
            Enums.AlertType result = 0;

            if ((flags | Enums.AlertActionFlag.BubbleAlert) == flags)
            {
                result = result | Enums.AlertType.CommonType;
            }
            if ((flags | Enums.AlertActionFlag.PromptAlert) == flags)
            {
                result = result | Enums.AlertType.CommonType;
            }
            if ((flags | Enums.AlertActionFlag.ManualSendAcrarsAlert) == flags)
            {
                result = result | Enums.AlertType.CommonType;
            }
            if ((flags | (Enums.AlertActionFlag.ConfirmAcars | Enums.AlertActionFlag.AutoSendAcarsAlert)) == flags)
            {
                result = result | Enums.AlertType.CommonType;
            }
            if ((flags | Enums.AlertActionFlag.ManualSendSMSAlert) == flags)
            {
                result = result | Enums.AlertType.SMSType;
            }
            if ((flags | (Enums.AlertActionFlag.ConfirmSMS | Enums.AlertActionFlag.AutoSendSMSAlert)) == flags)
            {
                result = result | Enums.AlertType.SMSType;
            }

            return result;
        }

        #endregion
    }
}
